<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*门店超级管理员管理管理*/
class ShopAdmin extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();
    }
	/*门店管理员列表*/
	function index(){
		$this->assign("shops",db("Shop")->cache("ShopsNameList",3600)->where("parent_id!=0")->order("parent_id desc,shop_name desc")->column("shop_name"));
		echo $this->fetch();
	}	
	//删除门店管理员
	function delete(){
		$id=input("id/d");
		db("Admin")->where("id =$id")->delete();
		$this->ajax_success("成功");
	}
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	function add(){
		$this->assign("shops",db("Shop")->where("parent_id>0")->column("id,shop_name"));
		echo $this->fetch();
	}
	function reset_psw(){
		$id=input("id/d");
		db("Admin")->where("id =$id")->setField(array("password"=>md5("123456")));
		$this->ajax_success("成功");
	}
	function add_post(){
		if(request()->isPost()){
			$flag=db("Admin")->where("account='".input('account')."'")->count(1);
			if($flag>0){
			 	$this->ajax_error("门店超级管理员账号已经存在");
			}
			$shop_id=db("Admin")->insert(array("name"=>input("name"),"account"=>input("account"),"password"=>md5(input("password")),"mobile"=>input("mobile"),'shop_id'=>input("shop_id"),"is_shop_admin"=>1));
		    $this->ajax_success("成功");
		}
	}
   /**验证管理员账号唯一**/
	function varify_uniq(){
		$account=input("account");
		$count=db("admin")->where("account='$account'")->count(1);
		if($count>0){
			$this->ajax_error("账号已经存在");
		}
	}
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10001001");
		$this->assign("menus",$auth_group);
	}
	
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
		 	'account'=>array("field" =>"a.account","operator" =>"like"),
		 	'shop_name'=>array("field" =>"b.shop_name","operator" =>"="),
		 );
		 $shop_id=sp_get_current_shop_id();
        $draw=input("draw",0)+1;//请求时间
		 if(!empty($shop_id)){
		 	return array('pageData'=>[],'total'=>0,"draw"=>$draw);
		 }

		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$where_and.=" and a.shop_id!=0 and a.is_shop_admin=1";
		$total=db("Admin")->alias("a")->join([["m_shop b","a.shop_id=b.id","left"]])->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("Admin")->field("a.*,b.shop_name")->alias("a")->join([["m_shop b","a.shop_id=b.id","left"]])->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
	
}
